<template>
	<view class="pages-a">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar :isBack="false" :bottomShadow="false" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<!-- <view class="custom-nav__back" @click="tn('/pageA/navigation/navigation')">
					<view class="tn-icon-deploy"></view>
				</view> -->
				<view class="tn-margin-top"
					style="text-shadow:  1rpx 0 0 #FFF, 0 1rpx 0 #FFF, -1rpx 0 0 #FFF , 0 -1rpx 0 #FFF;">
					<tn-tabs :list="scrollList" :current="current" @change="tabChange" activeColor="#000" :bold="true"
						:fontSize="36"></tn-tabs>
				</view>
			</view>
		</tn-nav-bar>

		<view class="" :style="{paddingTop: vuex_custom_bar_height + 'px'}">

			<view class="tn-color-gray--dark"
				style="margin: 20rpx 30rpx 0 30rpx;border-radius: 100rpx;padding-left: 6rpx;background-color: rgba(248, 247, 248, 0.9);"
				@click="navigateByLogin('/pageA/search/search')">
				<tn-notice-bar :list="vuex_hot_search_word?vuex_hot_search_word:[]" mode="vertical"
					leftIconName="search" :duration="6000"></tn-notice-bar>
			</view>

			<view style="margin-top: 30rpx;">
				<tn-swiper :list="swiperList" :effect3d="true" name="icon" :height="360" :title="false"
					titleName="title" :radius="12" mode="dot" :interval="5000" @click="swiperClick">
				</tn-swiper>
			</view>
			<!-- <swiper  class="card-swiper" :circular="true" :autoplay="true" duration="400" interval="8000"
				@change="cardSwiper">
				<swiper-item @click="tn('/pageA/atlas/atlas?id=' + item.id)" v-for="(item,index) in swiperList"
					:key="index" :class="cardCur==index?'cur':''">
					<view class="swiper-item image-banner"
						:style="'background-image:url('+ item.icon + ');background-size: cover;border-radius: 15rpx;'">
					</view> -->
			<!-- <view class="swiper-item-text">
						<view class="tn-text-bold tn-color-white" style="font-size: 50rpx;">{{item.title}}</view>
						<view class="tn-color-white tn-padding-top" style="font-size: 30rpx;">{{item.name}}</view>
						<view class="tn-text-sm tn-text-bold tn-color-white tn-padding-top-sm tn-padding-bottom-sm">
							{{item.text}}
						</view>
					</view> -->
			<!-- </swiper-item>
			</swiper> -->
			<!-- <view class="indication">
				<block v-for="(item,index) in swiperList" :key="index">
					<view class="spot" :class="cardCur==index?'active':''"></view>
				</block>
			</view> -->
		</view>


		<!-- 方式5，图片形式，安卓手机 start-->
		<view v-for="(colItem,colIndex) in marketingList" :key="colIndex" class="tn-flex tn-flex-wrap home-shadow"
			:class="colIndex===0?'tn-padding-top':''">
			<block v-for="(item, index) in colItem" :key="index">
				<view class="tn-margin-bottom tn-margin-top-sm" style="width: 25%;"
					@click="tn('/pageB/marketing/marketing?id='+item.id)">
					<view class="tn-flex tn-flex-direction-column tn-flex-row-center tn-flex-col-center">
						<view class="icon5__item--icon tn-flex tn-flex-row-center tn-flex-col-center"
							:style="'background-image:url('+ item.icon +');background-size:100% 100%;background-size: cover;'">
						</view>
						<view class="tn-color-black tn-text-center">
							<text class="tn-text-ellipsis">{{ item.name }}</text>
						</view>
					</view>
				</view>
			</block>
		</view>
		<!-- 方式5 end-->


		<!--胶囊 banner 需要可用显示出来即可 start-->
		<view class="tn-flex tn-flex-wrap tn-padding-bottom">
			<view class="" style="width: 100%;">
				<view class="image-piccapsule tn-shadow-blur"
					style="background-image:url('http://res.sunlee.icu/2023-07/23/f73128d245aa48b181b8b05b911d25fd.png');">
					<view class="image-capsule">
					</view>
				</view>
			</view>
		</view> 
		<!-- banner end -->


		<view class="">
			<view v-if="waterfallsFlowShow">
				<view class="" style="padding: 30rpx 20rpx;">
					<tn-waterfall ref="waterfall" v-model="list" @finish="handleWaterFallFinish">
						<template v-slot:left="{ leftList }">
							<view v-for="(item, index) in leftList" :key="index" class="wallpaper__item">
								<view @click="tn('/pageA/atlas/atlas?id=' + item.id)" class="item__image">
									<tn-lazy-load :threshold="6000" height="100%" :image="item.multiImg"
										:index="item.id" imgMode="widthFix"></tn-lazy-load>
								</view>
								<view class="item__data">

									<view @click="tn('/pageA/atlas/atlas?id=' + item.id)" class="item__title-container">
										<text class="item__title tn-color-black">{{ item.title }}</text>
									</view>

									<view class="item__tags-container">
										<view v-for="(tagItem, tagIndex) in item.tags" :key="tagIndex"
											:class="[item.color[tagIndex]]">{{tagItem}}
										</view>

									</view>

									<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xs">
										<view class="justify-content-item">
											<view v-if="item.author"
												@click="tn('/pageA/author/author?id='+item.author.userId)"
												class="tn-flex tn-flex-col-center tn-flex-row-left">
												<view class="logo-pic">
													<view class="logo-image">
														<view class=""
															:style="'background-image:url('+ item.author.headImg +');width: 40rpx;height: 40rpx;background-size: cover;'">
														</view>
													</view>
												</view>
												<view class="tn-padding-left-xs">
													<text
														class="tn-color-gray tn-text-sm">{{item.author.username}}</text>
												</view>

											</view>
										</view>
										<view class="justify-content-item">
											<text class="tn-icon-footprint tn-color-gray tn-padding-right-xs"></text>
											<text class="tn-color-gray">{{ item.viewCount }}</text>
										</view>
									</view>


								</view>
							</view>
						</template>
						<template v-slot:right="{ rightList }">
							<!--<view class="tn-color-black tn-text-bold tn-bg-yellow home-shadow"
								style="height: 160rpx;margin: 0 10rpx 20rpx 10rpx;border-radius: 10rpx;"
								@click="tn('/pageB/activity/activity')">
								 <view class="tn-padding-left tn-padding-top-lg">
									Ai · 广场
								</view>
								<view class="tn-padding-left tn-padding-top-xs">
									晒出你的ai创作
									<text class="tn-icon-right tn-padding-left-xs"></text>
								</view> 
							</view> -->
							<view v-for="(item, index) in rightList" :key="index" class="wallpaper__item">
								<view @click="tn('/pageA/atlas/atlas?id=' + item.id)" class="item__image">
									<tn-lazy-load :threshold="6000" height="100%" :image="item.multiImg"
										:index="item.id" imgMode="widthFix"></tn-lazy-load>
								</view>
								<view class="item__data">

									<view @click="tn('/pageA/atlas/atlas?id=' + item.id)" class="item__title-container">
										<text class="item__title tn-color-black">{{ item.title }}</text>
									</view>

									<view class="item__tags-container">
										<view v-for="(tagItem, tagIndex) in item.tags" :key="tagIndex"
											:class="[item.color[tagIndex]]">{{ tagItem }}</view>
									</view>

									<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xs">
										<view class="justify-content-item">
											<view v-if="item.author"
												@click="tn('/pageA/author/author?id='+item.author.userId)"
												class="tn-flex tn-flex-col-center tn-flex-row-left">
												<view class="logo-pic">
													<view class="logo-image">
														<view class=""
															:style="'background-image:url('+ item.author.headImg +');width: 40rpx;height: 40rpx;background-size: cover;'">
														</view>
													</view>
												</view>
												<view class="tn-padding-left-xs">
													<text
														class="tn-color-gray tn-text-sm">{{item.author.username}}</text>
												</view>

											</view>
										</view>

										<view class="justify-content-item">
											<text class="tn-icon-footprint tn-color-gray tn-padding-right-xs"></text>
											<text class="tn-color-gray">{{ item.viewCount }}</text>
										</view>

									</view>
								</view>
							</view>
						</template>
					</tn-waterfall>
				</view>
				<tn-load-more v-if="list.length>0" :status="loadStatus"></tn-load-more>
				<tn-empty v-else :textSize="30" :iconSize="160" mode="search" text="空空如也"></tn-empty>
			</view>

			<!-- <view class="" v-if="current==1">
				<view class="" style="padding: 6vh 20rpx;">
					<view class="tn-text-center" style="font-size: 200rpx;padding-top: 30rpx;">
						<text class="tn-icon-wea-wind tn-color-gray--light"></text>
					</view>
					<view class="tn-color-gray--disabled tn-text-center tn-text-lg">内容被台风吹走了</view>
				</view>
			</view>

			<view class="" v-if="current==2">
				<view class="" style="padding: 6vh 20rpx;">
					<view class="tn-text-center" style="font-size: 200rpx;padding-top: 30rpx;">
						<text class="tn-icon-wea-wind tn-color-gray--light"></text>
					</view>
					<view class="tn-color-gray--disabled tn-text-center tn-text-lg">内容被台风卷走了</view>
				</view>
			</view> -->

		</view>


		<!-- <view class='tn-tabbar-height'></view> -->

	</view>
</template>

<script>
	import {
		getHomeWaterfallsFlowData
	} from '@/api/product.js';

	import {
		getMarketingList,
		getCarouselProductList,
	} from '@/api/marketing.js';

	import {
		loginRouter
	} from "@/utils/loginUtil.js";

	import {
		randomColor
	} from "@/utils/colorUtil.js";
	export default {
		name: 'PagesA',
		data() {
			return {
				page: 0,
				limit: 10,
				endPageTotal: 0,
				// 当前排序类型
				currentSortType: 0,
				// 控制瀑布流刷新
				waterfallsFlowShow: true,
				marketingList: [],
				cardCur: 0,
				isAndroid: true,
				swiperList: [],
				current: 0,
				scrollList: [{
						sortType: 0,
						name: '最新'
					},
					{
						sortType: 1,
						name: '热门'
					},
					{
						sortType: 2,
						name: '推荐'
					}
				],
				/* 瀑布流*/
				loadStatus: 'loadmore',
				list: [],
				data: []
			}
		},
		created() {
			const systemInfo = uni.getSystemInfoSync()

			if (systemInfo.system.toLocaleLowerCase().includes('ios')) {
				this.isAndroid = false
			} else {
				this.isAndroid = true
			}

			// 轮播图
			this.getCarouselData()
			// 导航栏
			this.getMarketingData()
			// 瀑布流
			this.getRandomData()
		},
		methods: {
			navigateByLogin(url) {
				loginRouter(url)
			},
			// 点击轮播图
			swiperClick(index) {
				this.tn('/pageA/atlas/atlas?id=' + this.swiperList[index].id)
			},
			// 查询分类为首页导航
			getMarketingData() {
				getMarketingList(3).then(res => {
					let groupNum = 4;
					for (var i = 0, len = res.data.length; i < len; i += groupNum) {
						this.marketingList.push(res.data.slice(i, i + groupNum));
					}
				})
			},
			// 查询轮播图
			getCarouselData() {
				getCarouselProductList(1).then((res) => {
					this.swiperList = res.data.carouselProducts
				});
			},
			// cardSwiper
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			// tab选项卡切换
			tabChange(index) {
				this.waterfallsFlowShow = false
				this.current = index
				this.currentSortType = this.scrollList[index].sortType
				this.page = 0
				this.getRandomData()
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			// 查询瀑布流
			getRandomData() {
				if (this.loadStatus === 'nomore') {
					return
				}
				this.page++
				this.loadStatus = 'loading'
				getHomeWaterfallsFlowData({
					page: this.page,
					limit: this.limit,
					sortType: this.currentSortType
				}).then((res) => {
					const productData = res.data.list

					// 截取标签，并设置颜色
					if (productData.length > 0) {
						for (let item of productData) {
							if (item.tags && item.tags.length > 0) {
								item.tags = item.tags.slice(0, 3)
								item.color = []
								for (let index in item.tags) {
									item.color.push(randomColor())
								}
							}
						}
					}

					if (this.page === 1) {
						this.list = productData
					} else {
						this.list = this.list.concat(productData)
					}

					this.endPageTotal = productData.length
					this.waterfallsFlowShow = true
				});
			},
			// 瀑布流加载完毕事件
			handleWaterFallFinish() {
				if (this.endPageTotal < this.limit) {
					this.loadStatus = 'nomore'
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pages-a {
		max-height: 100vh;
	}


	/* 自定义导航栏内容 start */
	.custom-nav {
		height: 100%;

		&__back {
			margin: auto 5rpx;
			font-size: 50rpx;
			margin-right: 10rpx;
			margin-left: 30rpx;
			flex-basis: 5%;
		}
	}

	/* 底部安全边距 start*/
	.tn-tabbar-height {
		min-height: 120rpx;
		height: calc(140rpx + env(safe-area-inset-bottom) / 2);
		height: calc(140rpx + constant(safe-area-inset-bottom));
	}

	/* 图标容器5 start */
	.icon5 {
		&__item {
			// width: 30%;
			background-color: #FFFFFF;
			border-radius: 10rpx;
			padding: 0rpx;
			margin: 0rpx;
			transform: scale(1);
			transition: transform 0.3s linear;
			transform-origin: center center;

			&--icon {
				width: 96rpx;
				height: 96rpx;
				border-radius: 50%;
				margin-bottom: 18rpx;
				position: relative;
				z-index: 1;
			}
		}
	}

	/* 轮播视觉差 start */
	.card-swiper {
		height: 460rpx !important;
	}

	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		padding: 40rpx 30rpx 30rpx 30rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		border-radius: 15rpx;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		will-change: transform;
		// overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
		will-change: transform;
	}

	.card-swiper swiper-item .swiper-item-text {
		margin-top: -220rpx;
		text-align: center;
		width: 100%;
		display: block;
		border-radius: 10rpx;
		transform: translate(100rpx, 0rpx) scale(0.9, 0.9);
		transition: all 0.6s ease 0s;
		will-change: transform;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item-text {
		margin-top: -220rpx;
		width: 100%;
		transform: translate(0rpx, 0rpx) scale(0.9, 0.9);
		transition: all 0.6s ease 0s;
		will-change: transform;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -70rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 1;
		width: 30rpx;
		background-color: #FFFFFF;
	}


	/* 胶囊banner*/
	.image-capsule {
		padding: 100rpx 0rpx;
		font-size: 40rpx;
		font-weight: 300;
		position: relative;
	}

	.image-piccapsule {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 20rpx 20rpx 0 0;
	}

	/* 用户头像 start */
	.logo-image {
		width: 40rpx;
		height: 40rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 1rpx solid rgba(255, 255, 255, 0.05);
		// box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 瀑布流*/
	.wallpaper__item {
		background-color: #FFFFFF;
		overflow: hidden;
		margin: 0 10rpx;
		margin-bottom: 40rpx;
		// box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);

		.item {

			/* 图片 start */
			&__image {
				width: 100%;
				height: auto;
				background-color: #FFFFFF;
				border: 1rpx solid #F8F7F8;
				border-radius: 10rpx;
				overflow: hidden;
			}

			/* 图片 end */

			/* 内容 start */
			&__data {
				padding: 14rpx 0rpx;
			}

			/* 标题 start */
			&__title-container {
				text-align: justify;
				line-height: 38rpx;
				vertical-align: middle;
			}

			&__title {
				font-size: 30rpx;
			}

			/* 标题 end */

			/* 标签 start */
			&__tags-container {
				display: flex;
				flex-direction: row;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: flex-start;
			}

			&__tag {
				margin: 10rpx;
				color: #7C8191;
				background-color: #F3F2F7;
				padding: 4rpx 14rpx 6rpx;
				border-radius: 10rpx;
				font-size: 20rpx;

				&:first-child {
					margin-left: 0rpx !important;
				}
			}

			/* 标签 end */

			/* 内容 end */
		}
	}
</style>